package com.snail.auth.service;

import com.common.entity.Page;
import com.common.util.StringUtil;
import com.snail.auth.dao.AuthRoleGroupRep;
import com.snail.auth.entity.AuthRoleGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @description 角色分组
 * @date 2021/7/22
 * @autor houyanfeng
 */
@Service
@Transactional
public class AuthRoleGroupService {
    @Autowired
    private AuthRoleGroupRep roleGroupRep;

    public void save(AuthRoleGroup entity) {
        roleGroupRep.saveAndFlush(entity);
    }

    public AuthRoleGroup getByKey(Long id) {
        Optional optional = roleGroupRep.findById(id);
        if (optional.isPresent()) {
            return (AuthRoleGroup) optional.get();
        }
        return null;
    }

    public List<Map<String, Object>> list(Map<String, Object> params) {
        StringBuilder sql = new StringBuilder("select id, name from auth_role_group");
        if (!StringUtil.isEmpty(params.get("keyword"))) {
            sql.append(" and name like '%" + params.get("keyword").toString().trim() + "%'");
        }
        return roleGroupRep.execSql(sql.toString());
    }

    public Page page(Page page, Map<String, Object> params) {
        StringBuilder sql = new StringBuilder("select id, name from auth_role_group");
        if (!StringUtil.isEmpty(params.get("keyword"))) {
            sql.append(" and name like '%" + params.get("keyword").toString().trim() + "%'");
        }
        return roleGroupRep.execSqlPage(page, sql.toString());
    }

    public void del(Long id) {
        roleGroupRep.deleteById(id);
    }
}
